package com.accounting.client.frontend.common;

import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.CollectionUtil;
import corelib.client.resource.CommonResources;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.grid.SimpleDataGrid;
import corelib.client.ui.widget.grid.SimpleDataGridHandler;
import corelib.client.ui.widget.layout.RelativeRow;
import corelib.client.ui.widget.page.PageBase;
import corelib.client.ui.widget.panel.SimpleLayoutPanel;
import corelib.client.ui.widget.toolbar.SimpleRibbon;
import corelib.client.ui.widget.toolbar.SimpleRibbonButton;
import corelib.client.ui.widget.toolbar.SimpleRibbonButton.ButtonSize;

public abstract class SimpleTablePage<T> extends PageBase<Widget>{
	
	private int pageSize = 20;
	private int toolBarItemWidth = 100;
	
	private SimpleRibbon ribbon;
	private SimpleRibbonButton nextButton;
	private SimpleRibbonButton previousButton;
	
	private SimpleDataGrid<T> table;
	private TableHandler<T>   tableHandler;
	
	private int currentOffset;
	private int currentPageSize;

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setTableHandler(TableHandler<T> tableHandler) {
		this.tableHandler = tableHandler;
	}

	@Override
	protected Widget createWidget() {
		ribbon = new SimpleRibbon();
		table  = createTable();
		List<SimpleRibbonButton> ribbonItems = createRibbonItems(ribbon);
		if (!CollectionUtil.isEmpty(ribbonItems)){
			 ribbon.addSeparator(ButtonSize.Medium);
		}
		ribbon.addPushButton(new SimpleRibbonButton("Refresh",
		    CommonResources.INSTANCE.refresh32(),
		    ButtonSize.Medium,
		    new WidgetListener(){
				@Override
				public void valueUpdated(Widget widget) {
					load(0);
				}
		 	}
		 ));
		ribbon.addSeparator(ButtonSize.Medium);
		ribbon.addPushButton(previousButton = new SimpleRibbonButton("Previous",
			CommonResources.INSTANCE.refresh32(),
			ButtonSize.Medium,
			new WidgetListener(){
				@Override
				public void valueUpdated(Widget widget) {
					load(Math.max(currentOffset + pageSize, 0));
				}
			}
		));
		ribbon.addPushButton(nextButton = new SimpleRibbonButton("Next",
			CommonResources.INSTANCE.refresh32(),
			ButtonSize.Medium,
			new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				load(Math.max(currentOffset - pageSize, 0));
			}
		}));
		
		List<Widget> toolBarItems = createToolBarItems();
		if (!CollectionUtil.isEmpty(createToolBarItems())){
			 ribbon.addLineBreak();
			 RelativeRow row = new RelativeRow();
			 for (Widget widget : createToolBarItems()) {
				  row.add(widget, toolBarItemWidth)
				  	 .addSpace(8);
			 }
		}
		
		SimpleLayoutPanel layout = new SimpleLayoutPanel();
		layout.add(ribbon);
		layout.add(table);
		int height = CollectionUtil.isEmpty(toolBarItems) ? 80 : 103;
		layout.setWidgetTopHeight(ribbon, 0, Unit.PX, height, Unit.PX);
		layout.setWidgetTopBottom(table, height, Unit.PX, 0, Unit.PX);
		return layout;
	}
	
	protected SimpleDataGrid<T> createTable() {
		SimpleDataGrid<T> table = new SimpleDataGrid<T>(pageSize, tableHandler);
		return table;
	}
	
	protected abstract void load(int offset);
	protected abstract List<SimpleRibbonButton> createRibbonItems(SimpleRibbon ribbon);
	protected abstract List<Widget> createToolBarItems();
	
	private abstract class TableHandler<T> extends SimpleDataGridHandler<T> {
		
		private List<T> data;
		
		@SuppressWarnings("unused")
		public void setObjects(List<T> objects){
			TableHandler.this.data = objects;
			getGrid().setObjects(objects);
			
			currentPageSize = objects.size();
			previousButton.setEnabled(currentOffset > 0);
			nextButton.setEnabled(currentPageSize == pageSize);
		}
		
		@Override
		public List<T> getObjects() {
			return TableHandler.this.data;
		}

		@Override
		public void onObjectDoubleClicked(T obj) {
			onObjectClicked(obj);
		}
		
	}
}
